package cn.edu.cug.cs.gtl.plugins.catalog.tree;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.actions.AbstractAction;
import cn.edu.cug.cs.gtl.apis.ui.datake.DatakeApp;
import cn.edu.cug.cs.gtl.apis.ui.datake.DatakeFrame;
import cn.edu.cug.cs.gtl.apis.ui.datake.catalogtree.CatalogTree;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.event.ActionEvent;
import java.util.Locale;
import java.util.ResourceBundle;

public class DeleteSelectItemAction extends AbstractAction {
    @Override
    public void initialize(AbstractApplication theApp) {
        this.theApp = theApp;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("catalog-resource", Locale.getDefault());

        name = resourceBundle.getString("popupmenu.catalog.tree.delete_select_item");
        menuItem.setText(name);
        menuItem.addActionListener(this);
        ((CatalogTree) (getDatakeFrame().getCatalogPanel().getTreeComponent())).getItemNodePopupMenu().add(menuItem);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        CatalogTree tree = (CatalogTree) getDatakeFrame().getCatalogPanel().getTreeComponent();
        DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if(selectNode == null || selectNode.getParent() == null) return ;

        DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();
        treeModel.removeNodeFromParent(selectNode);
    }
    public DatakeFrame getDatakeFrame() {
        return (DatakeFrame) (((DatakeApp)theApp).getMainFrame());
    }
}
